package com.sne.airways.system.controller;

import com.sne.airways.basecore.controller.BaseController;
import com.sne.airways.basecore.exceptions.AppException;
import com.sne.airways.system.service.LoginService;
import com.sne.airways.system.utils.AppUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * User: skyline{http://my.oschina.net/skyline520}
 * Date: 12-11-16
 * Time: 下午3:58
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class LoginController extends BaseController {
    @Autowired
    private LoginService loginService;
    @RequestMapping("/")
    public String execute(){
        return "redirect:/";
    }
    @RequestMapping("/login")
    public String loginUser(HttpServletRequest request,String userNo,String password){
        try {
            loginService.processLogin(userNo,password);
            request.setAttribute("session", AppUtils.getSession());
        } catch (AppException e) {
            request.setAttribute("message",e.getMessage());
            return "/";
        }
        return "/main";
    }
    @RequestMapping("/main")
    public String redirectMain(HttpServletRequest request){
        try {
//            loginService.initSysOperatorOperation();
        } catch (Exception e) {
            return "error/exception";
        }
        return "main.jsp";
    }
    @RequestMapping("/test")
    public String test(){
        return "main.jsp";
    }
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        try {
            loginService.processLogout();
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return "redirect:/";
    }
}
